resource "alicloud_vpc" "vpc" {
  count             = var.vpc_id != "" ? 0 : var.create ? 1 : 0
  vpc_name          = var.vpc_name
  cidr_block        = var.vpc_cidr
  resource_group_id = var.resource_group_id
  description       = var.vpc_description
  tags = merge(
  {
    "Name" = format("%s", var.vpc_name)
  },
  var.vpc_tags,
  )
}

resource "alicloud_vswitch" "main" {
  count      = length(var.vswitch_cidrs)
  vpc_id     = var.vpc_id != "" ? var.vpc_id : concat(alicloud_vpc.vpc.*.id, [""])[0]
  vswitch_name = format("%s%03d", var.vswitch_name, count.index + 1)

  cidr_block = element(var.vswitch_cidrs, count.index)
  zone_id    = element(var.availability_zones, count.index)

  tags = merge(
  {
    "Name" = format("%s%03d", var.vswitch_name, count.index+1)
  },
  var.vswitch_tags,
  )

  depends_on = [alicloud_vpc.vpc]
}